package com.newbingo.aop;

import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.MethodBeforeAdvice;
import javax.servlet.http.HttpSession;

import com.newbingo.dao.imple.IAgentDao;
import com.newbingo.dao.imple.IAgentLogDao;
import com.newbingo.dto.AgentDto;
import com.newbingo.entity.AgentLog;
import com.newbingo.service.imple.IAgentLogService;
import com.newbingo.service.imple.IAgentService;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

/**
 * 代理的日志，为Agent调用方法后置通知，进行记录历史操作 调用前置通知
 * 
 * @author ChansonYan
 */

public class AgentLogAop implements MethodBeforeAdvice {
	private IAgentLogDao agentLogDao;
	private IAgentDao agentDao;

	public void before(Method m, Object[] args, Object target) throws Throwable {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(request!=null){
			HttpSession session = request.getSession();
			if(session!=null){
				Object object = session.getAttribute("user");
				if (object != null) {
					if (object instanceof AgentDto) {
						AgentDto agentDto = (AgentDto) object;
						AgentLog agentLog = new AgentLog();
						agentLog.setAgent(agentDao.findById(agentDto.getUno()));
						agentLog.setOperateTime(new Date());
						agentLog.setContents("调用了类" + target.getClass().getName()
								+ "方法" + m.getName());
						agentLogDao.save(agentLog);
					}
				}
			}
		}
	}

	public IAgentLogDao getAgentLogDao() {
		return agentLogDao;
	}

	public void setAgentLogDao(IAgentLogDao agentLogDao) {
		this.agentLogDao = agentLogDao;
	}

	public IAgentDao getAgentDao() {
		return agentDao;
	}

	public void setAgentDao(IAgentDao agentDao) {
		this.agentDao = agentDao;
	}


}
